package cn.com.code.design;

import java.util.concurrent.atomic.AtomicReference;

/**
 * CAS AtomicReference 线程安全
 *
 * @author zhengpei
 * @date 2021/9/17
 */
public class Singleton06 {

  private static final AtomicReference<Singleton06> INSTANCE = new AtomicReference<Singleton06>();

  private Singleton06() {

  }

  public static final Singleton06 getInstance() {

    for (; ; ) {
      Singleton06 instance = INSTANCE.get();
      if (null != instance) {
        return instance;
      }
      INSTANCE.compareAndSet(null ,new Singleton06());
      return INSTANCE.get();
    }

  }

}
